<template>
  <a-modal
    v-model="visible"
    title="填写密码信息"
    :destroyOnClose="true"
    @ok="handleOk"
    :confirm-loading="confirmLoading"
    @cancel="$emit('update:visible', false)"
  >
    <a-form :form="form" :label-col="{ span: 7 }" :wrapper-col="{ span: 14 }">
      <a-form-item label="密码类型">
        <a-select
          v-decorator="[
            'passwordType',
            {
              initialValue: 0,
              rules: [
                {
                  required: true,
                  message: '密码类型不能为空'
                }
              ]
            }
          ]"
          placeholder="请选择"
        >
          <a-select-option :value="item.id" v-for="item in typeArr" :key="item.id">{{ item.name }}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="申报密码" v-if="form.getFieldValue('passwordType') == 0">
        <a-input
          placeholder="请输入密码"
          :maxLength="50"
          v-decorator="['declarePassword', { rules: [{ required: true, message: '申报密码不能为空' }] }]"
        />
      </a-form-item>
      <a-form-item label="实名账号" v-if="form.getFieldValue('passwordType') == 2">
        <a-input
          placeholder="请输入账号"
          :maxLength="50"
          v-decorator="['realNameAccount', { rules: [{ required: true, message: '实名账号不能为空' }] }]"
        />
      </a-form-item>
      <a-form-item label="实名密码" v-if="form.getFieldValue('passwordType') == 2">
        <a-input
          placeholder="请输入密码"
          :maxLength="50"
          v-decorator="['realNamePassword', { rules: [{ required: true, message: '实名密码不能为空' }] }]"
        />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script>
import { postCalculateTax, calculateBatchesGet, calculateTaxFeedBack } from "./api";
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    selectedRowKeysId: {
      type: Array,
      default: () => []
    },
    selectedRowKeys: {
      type: Array,
      default: () => []
    },
    text: {
      type: String,
      default: ""
    }
  },
  watch: {
    visible(v) {
      if (v) {
        this.form.resetFields();
        this.calculateBatchesGetFun();
        setTimeout(() => {
          this.form.setFieldsValue({
            passwordType: 0
          });
        }, 500);
      }
    }
  },
  data() {
    return {
      calculateTax: {},
      confirmLoading: false,
      form: this.$form.createForm(this, {}),
      typeArr: [
        { id: 0, name: "申报密码" },
        { id: 2, name: "实名密码" }
      ]
    };
  },
  created() {},
  methods: {
    calculateBatchesGetFun() {
      calculateBatchesGet(this.$route.query.id)
        .then((res) => {
          // console.log(res);
          this.calculateTax = res.data;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    async onPutUpdateBatch(v) {
      try {
        this.confirmLoading = true;
        const data = null;
        if (this.text === "算税") {
          await postCalculateTax(v);
        } else {
          await calculateTaxFeedBack(v);
        }

        this.confirmLoading = false;
        this.$notification["success"]({
          message: "操作成功"
        });

        this.$emit("update:visible", false);
        this.$emit("upDate", data);
      } catch (err) {
        console.log("error log --> ", err);
        this.confirmLoading = false;
      }
    },
    handleOk() {
      // 编辑金额
      this.form.validateFields((err, values) => {
        if (!err) {
          const v = {
            companyAuthId: this.calculateTax.authId,
            batchId: this.calculateTax.id,
            period: this.calculateTax.period,
            ...values
          };
          this.onPutUpdateBatch(v);
        }
      });
    }
  }
};
</script>

<style></style>
